package com.hc.comm.filter;



import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.hc.comm.utils.Constant;
import com.hc.comm.utils.PropertiesCacheUtil;



public class DefaultFilter implements Filter {

	private Logger logger = Logger.getLogger(DefaultFilter.class);

	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		long startTime = System.currentTimeMillis();
		HttpServletRequest rs = (HttpServletRequest)request;
		logger.info(rs.getRequestURI());
		logger.info("time****:"+System.currentTimeMillis());
		initFilter(request, response);
		logger.info("time****:"+System.currentTimeMillis());
		chain.doFilter(request, response);
		logger.info("time****:"+System.currentTimeMillis());
		logger.info(String.format("本次请求[  %s   ]耗时(ms):%s",
				((HttpServletRequest) request).getRequestURL(),
				(System.currentTimeMillis() - startTime)));
	}

	private void initFilter(ServletRequest request, ServletResponse response) {
		request.setAttribute("FPWEB", PropertiesCacheUtil
				.get(Constant.PropertiesFileEnum.WEB.getFilename()));
		request.setAttribute("FPWEB_JSON", PropertiesCacheUtil
				.getJson(Constant.PropertiesFileEnum.WEB.getFilename()));
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {

	}

}
